package start.spring.basic.job.quartz;

import java.util.Properties;

import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * ****************************************************************************
 * 计划任务配置类
 *
 * @author(作者)：xuyongyun	
 * @date(创建日期)：2018年8月30日
 ******************************************************************************
 */
@Configuration
@PropertySource("classpath:quartz.properties")
public class SchedulerConfig {
	
	@Autowired
	private SpringJobFactory springJobFactory;
	
	@Value("${org.quartz.threadPool.threadCount}")
	private int threadCount;
	
	 
	@Bean
	public SchedulerFactoryBean schedulerFactoryBean() {
		SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
	    schedulerFactoryBean.setJobFactory(springJobFactory);
	    
	    Properties prop = new Properties();
	    
	    prop.put("org.quartz.scheduler.instanceName", "DefaultQuartzScheduler");
	    prop.put("org.quartz.scheduler.rmi.export", "false");
	    prop.put("org.quartz.scheduler.rmi.proxy", "false");
	    prop.put("org.quartz.scheduler.wrapJobExecutionInUserTransaction", "false");
        
        prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
        prop.put("org.quartz.threadPool.threadCount", String.valueOf(threadCount));
        prop.put("org.quartz.threadPool.threadPriority", "5");
        prop.put("org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread", "true");
        prop.put("org.quartz.jobStore.misfireThreshold", "60000");
        prop.put("org.quartz.jobStore.class", "org.quartz.simpl.RAMJobStore");

        schedulerFactoryBean.setQuartzProperties(prop);
        
	    return schedulerFactoryBean;
	}
	
	@Bean(name = "scheduler")
	public Scheduler scheduler() {
		return schedulerFactoryBean().getScheduler();
	}
}
